﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Data;

namespace POSSystem.BLL
{
    public class View_RepairJobs : _View_RepairJobs
    {
        public static View_RepairJobs GetCurrentJobs()
        {
            try
            {
                View_RepairJobs job = new View_RepairJobs();
                
                job.Query.OpenParenthesis();
                job.Where.StatusID.Value = RepairStatus.Returned;
                job.Where.StatusID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.NotEqual;

                MyGeneration.dOOdads.WhereParameter whereStatusID = job.Where.TearOff.StatusID;
                whereStatusID.Conjuction = MyGeneration.dOOdads.WhereParameter.Conj.Or;
                whereStatusID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.IsNull;
                job.Query.CloseParenthesis();
                job.Where.BranchID.Value = Branches.CurrentBranchID;
                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_RepairJobs.cs", "GetCurrentJobs", ex.StackTrace);
                throw ex;
            }
        }

        public static View_RepairJobs GetJobsByIMEI(string imei)
        {
            try
            {
                View_RepairJobs job = new View_RepairJobs();
                job.Where.IMEI.Value = imei;
                job.Where.BranchID.Value = Branches.CurrentBranchID;
                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_RepairJobs.cs", "GetJobsByIMEI", ex.StackTrace);
                throw ex;
            }
        }

        public static View_RepairJobs GetDoneOrRejectedJobs()
        {
            try
            {
                View_RepairJobs job = new View_RepairJobs();
                job.Query.OpenParenthesis();
                job.Where.StatusID.Value = RepairStatus.Done;
                
                MyGeneration.dOOdads.WhereParameter whereStatusID = job.Where.TearOff.StatusID;
                whereStatusID.Conjuction = MyGeneration.dOOdads.WhereParameter.Conj.Or;
                whereStatusID.Value = RepairStatus.Rejected;
                job.Query.CloseParenthesis();
                job.Where.BranchID.Value = Branches.CurrentBranchID;
                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_RepairJobs.cs", "GetDoneOrRejectedJobs", ex.StackTrace);
                throw ex;
            }
        }

        public static View_RepairJobs GetJobByID(int id)
        {
            try
            {
                View_RepairJobs job = new View_RepairJobs();
                job.Where.ID.Value = id;

                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_RepairJobs.cs", "GetCurrentJobs", ex.StackTrace);
                throw ex;
            }
        }

        public static View_RepairJobs GetJobByRepairBox(int id)
        {
            try
            {
                View_RepairJobs job = new View_RepairJobs();
                job.Where.RepairBox.Value = id;

                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_RepairJobs.cs", "GetCurrentJobs", ex.StackTrace);
                throw ex;
            }
        }

        public virtual bool GetAllActiveJobByRepairBoxID(int boxID)
        {
            ListDictionary parameters = new ListDictionary();

            parameters.Add(new SqlParameter("@boxID", SqlDbType.Int, 0), boxID);

            return LoadFromSql("proc_GetAllActiveJobByRepairBoxID", parameters);

        }

        public static View_RepairJobs UpdateRepairBoxStatus(int id)
        {
            try
            {
                View_RepairJobs job = new View_RepairJobs();
                job.GetAllActiveJobByRepairBoxID(id);

                RepairBoxes.UpdateStatus(id, job.RowCount <= 0);

                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_RepairJobs.cs", "GetCurrentJobs", ex.StackTrace);
                throw ex;
            }
        }

        public static View_RepairJobs GetJobByNo(string repairNo)
        {
            try
            {
                View_RepairJobs job = new View_RepairJobs();
                job.Where.RepairNo.Value = repairNo;

                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_RepairJobs.cs", "GetJobByNo", ex.StackTrace);
                throw ex;
            }
        }

        public static View_RepairJobs GetJobByModelID(int modelID)
        {
            try
            {
                View_RepairJobs job = new View_RepairJobs();
                job.Where.RepairModelID.Value = modelID;
                job.Where.BranchID.Value = Branches.CurrentBranchID;
                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_RepairJobs.cs", "GetJobByNo", ex.StackTrace);
                throw ex;
            }
        }

        public static View_RepairJobs GetJobsByCustomerID(int customerID)
        {
            try
            {
                View_RepairJobs job = new View_RepairJobs();
                job.Where.CustomerID.Value = customerID;
                job.Where.BranchID.Value = Branches.CurrentBranchID;
                job.Query.AddOrderBy(View_RepairJobs.ColumnNames.CreatedDate, MyGeneration.dOOdads.WhereParameter.Dir.DESC);
                job.Query.Load();
                return job;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("View_RepairJobs.cs", "GetJobsByCustomerID", ex.StackTrace);
                throw ex;
            }
        }
    }
}
